<template>
  <!-- 来料检任务清单 -->
  <p-card :bordered="false">
    <!-- 进料检验类型开始 -->

    <!--<iframe  src="https://dev.iiot.hxct.com/srm-web/work/auto/SrmPcShorderList" frameborder="0" width="100%" height="801px" scrolling="auto"></iframe>-->

    <p-form-model
      ref="baseInfoData"
      :model="baseInfoData"
      :rules="rules"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
    >
      <p-page-header style="border: 1px solid rgb(235, 237, 240)" title="供应商出厂检验操作">
        <template slot="extra">
          <!--<p-button key="1" type="primary" @click="saveDataBtn">保存</p-button>-->

          <p-popconfirm
                  v-if="baseInfoData.inspectResult==1&&finalFlag==false"
            title="确定要提交吗？"
            ok-text="确定"
            cancel-text="取消"
            @confirm="submitData"
            @cancel="cancel"
          >
            <p-button type="primary">提交</p-button>
          </p-popconfirm>

          <!--<p-button @click="backToIndex">取消</p-button>-->

          <!-- <p-button key="1" type="primary"  @click="submitData">
             提交
          </p-button>-->
        </template>
      </p-page-header>
       <h4 class="title">基础信息</h4>
     <!-- <p-alert message="基础信息" type="info" />-->
      <p-row :gutter="24" style="margin-top:15px;">

        <p-col :span="8">
          <p-form-model-item ref="sendBillno" label="送货单号" prop="sendBillno">
            <p-input
                    readOnly
              v-model="baseInfoData.sendBillno"
              @blur="
          () => {
            $refs.sendBillno.onFieldBlur();
          }
        "
            />
          </p-form-model-item>
        </p-col>

        <p-col :span="8">
          <p-form-model-item ref="sendBillnoLine" label="送货单号行号 " prop="sendBillnoLine">
            <p-input
                    readOnly
                    v-model="baseInfoData.sendBillnoLine"
            />
          </p-form-model-item>
        </p-col>

        <p-col :span="8">
          <p-form-model-item ref="mcode" label="物料编号" prop="mcode">
            <p-input
                    readOnly
              v-model="baseInfoData.mcode"
              @blur="
          () => {
            $refs.mcode.onFieldBlur();
          }
        "
            />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item ref="mname" label="物料名称" prop="mname">
            <p-input
                    readOnly
              v-model="baseInfoData.mname"
            >
              <p-tooltip slot="suffix" :title="baseInfoData.mname">
                <p-icon type="info-circle" style="color: rgba(0,0,0,.45)" />
              </p-tooltip>
            </p-input>
          </p-form-model-item>
        </p-col>

        <p-col :span="8">
          <p-form-model-item ref="sampingMethod" label="抽检方式" prop="sampingMethod">
            <p-input
                    v-if="baseInfoData.sampingMethod==1"
                    readOnly

                    v-model="baseInfoData.sampingMethodText"
            />
            <p-input
                    v-if="baseInfoData.sampingMethod==2"
                    readOnly

              v-model="baseInfoData.aqlSimpleStandardName"
            />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item ref="simpleQty" label="抽检数量" prop="simpleQty">
            <p-input  type="number" v-model="baseInfoData.simpleQty" />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item ref="matSn" label="物料管理方式" prop="matSn">
            <p-input
                    readOnly

              v-model="baseInfoData.matSnText"
              @blur="
          () => {
            $refs.matSn.onFieldBlur();
          }
        "
            />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item ref="custno" label="供应商编码" prop="custno">
            <p-input
                    readOnly
              v-model="baseInfoData.custno"
              @blur="
          () => {
            $refs.custno.onFieldBlur();
          }
        "
            />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item ref="custname" label="供应商名称" prop="custname">
            <p-input
                    readOnly
              v-model="baseInfoData.custname"
            >
              <p-tooltip slot="suffix" :title="baseInfoData.custname">
                <p-icon type="info-circle" style="color: rgba(0,0,0,.45)" />
              </p-tooltip>
            </p-input>
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item ref="sendInspectTime" label="送检时间" prop="sendInspectTime">
            <p-input
                    readOnly
              v-model="baseInfoData.sendInspectTime"
              @blur="
          () => {
            $refs.sendInspectTime.onFieldBlur();
          }
        "
            />
          </p-form-model-item>
        </p-col>
        <p-col :span="8">
          <p-form-model-item ref="sendInspectQty" label="送检数量" prop="sendInspectQty">
            <p-input
                    readOnly
              v-model="baseInfoData.sendInspectQty"
              @blur="
          () => {
            $refs.sendInspectQty.onFieldBlur();
          }
        "
            />
          </p-form-model-item>
        </p-col>

        <p-col :span="8" v-if="visible==1">
          <p-form-model-item ref="questionRemark" label="问题备注" prop="questionRemark">
            <p-input v-model="baseInfoData.questionRemark" />
          </p-form-model-item>
        </p-col>
      </p-row>
      <p-row>
        <p-col :span="5">
          <p-tag>物料SN详情*</p-tag>
          <a @click="openProSNDetailBtn">查询</a>
        </p-col>

        <p-col :span="4">
          <p-tag>检验结论*</p-tag>
          <p-span v-if="baseInfoData.inspectResult==1" style="color:blue">合格</p-span>
          <p-span v-else-if="baseInfoData.inspectResult==2" style="color:red">不合格</p-span>
        </p-col>
      </p-row>
    </p-form-model>
    <p-page-header style="border: 1px solid rgb(235, 237, 240)" title="检验项目">
      <template slot="extra">
        <p-button key="1" type="primary" @click="addCheckProjectBtn">新增行</p-button>
      </template>
    </p-page-header>
    <p-table
      :columns="modal1InspectionColumns"
      :data-source="checkDataSource"
      class="components-table-demo-nested"

      bordered
      :expandIcon="expandIcon"
      style="margin-top:10px;"
    >
      <a slot="photoUrl" slot-scope="text">photoUrl</a>

      <template slot="operation" slot-scope="text,record">
        <p-popconfirm
          title="确定要删除此项？"
          ok-text="删除"
          cancel-text="取消"
          @confirm="deleteCheckProject(record.id)"
        >
          <a href="#">删除</a>
        </p-popconfirm>
      </template>

      <p-table
        slot="expandedRowRender"
        slot-scope="text"
        :columns="modal1InspectionInnerColumns"
        :data-source="text.detailList"
        :pagination="false"
        bordered
      >

        <!--
                <template slot="standardValue" slot-scope="text,record">
                  <p-select
                    v-if="record.valueType==1"
                    v-model="record.standardValue"
                    placeholder="please select your zone"
                  >
                    <p-select-option
                      v-for="(item,index) in passNgOptions"
                      :key="index"
                      :value="item.value"
                    >{{item.name}}</p-select-option>
                  </p-select>
                  <p-input
                    v-else-if="record.valueType==''||record.valueType==2"
                    v-model="record.standardValue"
                  />
                </template>

                <template slot="upperValue" slot-scope="text,record">
                  <p-input type="number" v-model="record.upperValue" />
                </template>

                <template slot="downValue" slot-scope="text,record">
                  <p-input type="number" v-model="record.downValue" />
                </template>

                <template slot="param" slot-scope="text,record">
                  <p-input v-model="record.param" />
                </template>-->

        <template slot="measuredValue" slot-scope="text,record">
          <p-select
            v-if="record.valueType==2"
            v-model="record.measuredValue" style="width: 100px;"
            placeholder="please select your zone"
            @change="individualJudgmentChange"
          >
            <p-select-option
              v-for="(item,index) in passNgOptions"
              :key="index"
              :value="item.value"
            >{{item.name}}</p-select-option>
          </p-select>
          <p-input
            v-else
            v-model="record.measuredValue " style="width: 100px;"
            type="number"
            @change="individualJudgmentChange"
          />
        </template>

        <!--<template slot="individualJudgment" slot-scope="text,record">
          <p-select
            v-model="record.individualJudgment"
            placeholder="please select your zone"
            @change="individualJudgmentChange"
          >
            <p-select-option
              v-for="(item,index) in measuredValueOptions"
              :key="index"
              :value="item.value"
            >{{item.name}}</p-select-option>
          </p-select>
        </template>-->

        <template slot="defectLevel" slot-scope="text,record">
          <p-select v-model="record.defectLevel" placeholder="please select your zone">
            <p-select-option
              v-for="(item,index) in questionLevelList"
              :key="index"
              :value="parseInt(item.value)"
            >{{item.title}}</p-select-option>
          </p-select>
        </template>
      </p-table>

      <template slot="photoUrl" slot-scope="text">
        <p-button>{{text.photoUrl}}</p-button>
      </template>
    </p-table>

    <!-- 进料检验类型结束 -->

    <div>
      <p-modal
        :visible="addCPModalVisible"
        width="60vw"
        title="新增行-检验项目"
        @ok="sureAddCheckProject"
        @cancel="closeAddCPModal"
        okText="添加"
        cancelText="取消"
      >
        <p-alert message="请勾选包含检验明细的检验项目" type="info" v-if="selectedType==1" show-icon />
        <p-alert message="满足条件，点击“添加”按钮插入" type="success" v-if="selectedType==2" show-icon />
        <p-alert message="该项没有对应的明细列表" type="warning" v-if="selectedType==3" show-icon />

        <h4 class="title"></h4>
        <p-table
          :data-source="templateList"
          :columns="templateListColumns"
          :rowSelection="{ selectedRowKeys: selectedRowKeysArray, onChange: onSelectChange, type: 'radio' }"
        ></p-table>

        <h4 class="title"></h4>
        <p-table :data-source="templateListDetails" :columns="templateListDetailsColumns"></p-table>
      </p-modal>



      <p-modal
              :visible="proSnDetailVisible"
              :width="800"
              title="物料SN详情"
             :footer="null"
              @cancel="closeProSnDetail"

      >
        <p-table :data-source="proSnDetailData" :columns="tproSnDetailColumns" bordered></p-table>

      </p-modal>


      <p-modal v-model="tipsModalVisible" title="提示" :footer="null">
        <p-result title="该信息已提交至Mes质量来料检">
          <template #extra>

          </template>
        </p-result>
      </p-modal>
    </div>

    <!-- 查询区域-END -->
  </p-card>
</template>

<script>
// import { JeecgListMixin } from "@/mixins/JeecgListMixin";
// import { api } from "@/api/manage";
// import JSuperQuery from "@/components/jeecg/JSuperQuery.vue";
// import { filterObj } from "@/utils/util";
// import MesQcIncomeBasicInfoModal from "./modules/MesQcIncomeBasicInfoModal";
// import Vue from "vue";
// import MesQcIncomeCheckProjectList from "./MesQcIncomeCheckProjectList";
// import MesQcIncomeFailList from "./MesQcIncomeFailList";
// import {
//   initDictOptions,
//   filterMultiDictText,
// } from "@/components/dict/JDictSelectUtil";
// import ButtonExpHandler from "@/components/jeecgbiz/modules/ButtonExpHandler";
// import { postAction, getAction, deleteAction } from "@/api/manage";
// import FxCompontensModal from "@/components/jeecgbiz/FxCompontensModal";

import { getAction, deleteAction, putAction,postAction } from "../../../api/manage";
import MesBcTemplateList from "./MesBcTemplateList";
import  dayjs   from 'dayjs';
import Vue from 'vue'
export default {
  name: "MesIncomeOperation",
  components: { MesBcTemplateList },
  props:['paramData'],

  // mixins: [JeecgListMixin],
  // components: {
  //   JSuperQuery,
  //   MesQcIncomeCheckProjectList,
  //   MesQcIncomeFailList,
  //   FxCompontensModal,
  //   MesQcIncomeBasicInfoModal,
  // },
  data() {
    return {


      labelCol: { span: 8 },
      wrapperCol: { span: 14 },

      baseInfoId: {},
      inMethod: {},

      visible: "1",
      //来料操作弹窗
      modal1Visible: false,
      //新增行弹出框
      addCPModalVisible: false,

      proSnDetailVisible:false,

      finalFlag:false,

      templateList: [],
      templateListDetails: [],
      selectedRowKeysArray: [],
      selectedRows: [],
      selectedType: 1,

      questionLevelList:[],
      questionLevelMap:[],

      basicResult: 1,
      baseInfoData: {},
      templateListColumns: [
        { title: "抽样样本", dataIndex: "sample", key: "sample" },
        { title: "物料编码", dataIndex: "mcode", key: "mcode" },
        { title: "物料名称", dataIndex: "mname", key: "mname" },
      ],
      proSnDetailData:[],
      tipsModalVisible:false,

      sampingMethodList:[],
      sampingMethodMap:[],
      tproSnDetailColumns:[
        {
          title: "序号",
          dataIndex: "key",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
        {
          title: "SN",
          dataIndex: "sn",
          key: "sn",
        },
      ],

      templateListDetailsColumns: [
        {
          title: "检验项目",
          dataIndex: "inspectItem",
          key: "inspectItem",
        },
        {
          title: "检验标准",
          dataIndex: "inspectStandard",
          key: "inspectStandard",
        },
        {
          title: "值类型",
          dataIndex: "valueType",
          key: "valueType",
          customRender: (text) => {
            if (!text) {
              return "";
            } else {
              return text == 1 ? "计量型" : "计数型";
            }
          },
        },
        { title: "标准值", dataIndex: "standardValue", key: "standardValue" },
        { title: "上限值", dataIndex: "upperValue", key: "upperValue" },
        { title: "下限值", dataIndex: "downValue", key: "downValue" },
      ],

      rules: {
        inspectBillno: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        sendBillno: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        workBillno: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        sendBillnoLine: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        mcode: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        mname: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        mbig: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        msmall: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        sampingMethod: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        simpleQty: [
          {
            required: true,
            message: "请输入抽检数量！",
            trigger: "blur",
          },
        ],
        matSn: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        custno: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        custname: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        sendInspectTime: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        sendInspectQty: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        checkBy: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        sendInspectBy: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        inMethod: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],

        currentAddress: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        custReport: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        tcsp: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        mSnDetail: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        photoUrl: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
        inspectResult: [
          {
            required: true,
            message: "Please input Activity name",
            trigger: "blur",
          },
        ],
      },
      checkDataSource:[],
      // 进料检验类型数据
      modal1InspectionColumns: [
        {
          title: "序号",
          dataIndex: "key",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
        { title: "抽样样本", dataIndex: "sample", key: "sample" },
        { title: "物料编码", dataIndex: "mcode", key: "mcode" },
        { title: "物料名称", dataIndex: "mname", key: "mname" },
        { title: "物料一级分类", dataIndex: "productOneType", key: "productOneType" },
        { title: "物料二级分类", dataIndex: "productTwoType", key: "productTwoType" },
        { title: "物料三级分类", dataIndex: "productThreeType", key: "productThreeType" },

        {
          title: "检验结果",
          dataIndex: "inspectResult",
          key: "inspectResult",
          customRender: (text) => {
            if (!text) {
              return "";
            } else {
              return text == 1 ? "合格" : "不合格";
            }
          },
        },
        {
          title: "图片",
          key: "photoUrl",
          scopedSlots: { customRender: "photoUrl" },
        },
        {
          title: "操作",
          key: "id",
          dataIndex: "id",
          scopedSlots: { customRender: "operation" },
        },
      ],

      modal1InspectionInnerColumns: [
        {
          title: "序号",
          dataIndex: "key",
          customRender: function (t, r, index) {
            return parseInt(index) + 1;
          },
        },
        { title: "检验项目", dataIndex: "inspectItem", key: "inspectItem" },
        {
          title: "检验标准",
          dataIndex: "inspectStandard",
          key: "inspectStandard",
        },
        {
          title: "值类型",
          key: "valueType",
          dataIndex: "valueType",

          customRender: (text) => {
            if (!text) {
              return "";
            } else {
              return text == 1 ? "计量型" : "计数型";
            }
          },
        },
        {
          title: "标准值",
          dataIndex: "standardValue",
          key: "standardValue",
          scopedSlots: { customRender: "standardValue" },
        },
        {
          title: "上限值",
          dataIndex: "upperValue",
          key: "upperValue",
          scopedSlots: { customRender: "upperValue" },
        },
        {
          title: "下限值",
          dataIndex: "downValue",
          key: "downValue",
          scopedSlots: { customRender: "downValue" },
        },
        {
          title: "参数",
          dataIndex: "param",
          key: "param",
          scopedSlots: { customRender: "param" },
        },
        {
          title: "实测值",
          dataIndex: "measuredValue",
          key: "measuredValue",
          scopedSlots: { customRender: "measuredValue" },
        },
        {
          title: "单项判定",
          dataIndex: "individualJudgment",
          key: "individualJudgment",
          customRender: (text) => {
            if (!text) {
              return "";
            } else {
              return text == 1 ? "PASS" : "NG";
            }
          },
        },
        {
          title: "缺陷等级",
          dataIndex: "defectLevel",
          key: "defectLevel",
          scopedSlots: { customRender: "defectLevel" },
        },

      ],

      valueTypeOptions: [
        { value: 1, name: "计数型" },
        { value: 2, name: "计量型" },
      ],

      passNgOptions: [
        {
          name: "PASS",
          value: "PASS",
        },
        {
          name: "NG",
          value: "NG",
        },
      ],
      measuredValueOptions: [
        {
          name: "pass",
          value: 1,
        },
        {
          name: "NG",
          value: 2,
        },
      ],
      defectLevelOptions: [
        { name: "空", value: 1 },
        { name: "A", value: 2 },
        { name: "B", value: 3 },
        { name: "C", value: 4 },
        { name: "D", value: 5 },
      ],
      // 直发协同类型数据
      modal2Visible: false,
      baseInfoData2: {
        //基础信息
        inspectBillno: "001", //送检单号
        sendBillno: "", //送货单号
        sendBillnoLine: "001", //送货单行号
        mcode: "", //物料编号
        mname: "", //物料名称
        mbig: "", //物料大类名称
        msmall: "", //物料小类名称
        sampingMethod: "", //抽检方式
        simpleQty: "3", //抽检数量
        matSn: "2020-01-01", //物料管理方式
        custno: "", //供应商编码
        custname: "", //供应商名称
        sendInspectTime: 1, //送检时间
        sendInspectQty: 3, //送检数量
        checkBy: "fdafsdd", //检验人
        inMethod: "", //来料方式
        custReport: "", //供应商报告
        tcsp: "", //技术规格书
        mSnDetail: "", //物料SN详情
        photoUrl: "", //封样图片
        inspectResult: "", //检验结论
        operation: 1, //操作
        inspectMethod: 1, //检验方式
        questionRemark: "", //备注
        fileUrl: "", //附件
      },
      modal2InspectionColumns: [
        {
          title: "序号",
          dataIndex: "key",
        },
        { title: "抽样样本", dataIndex: "sample", key: "sample" },
        { title: "物料编码", dataIndex: "mcode", key: "mcode" },
        { title: "物料名称", dataIndex: "mname", key: "mname" },
        { title: "物料小类", dataIndex: "subcategory", key: "subcategory" },
        { title: "物料大类", dataIndex: "mcategory", key: "mcategory" },
        {
          title: "检验结果",
          dataIndex: "inspectResult",
          key: "inspectResult",
          customRender: (text) => {
            if (!text) {
              return "";
            } else {
              return text == 1 ? "合格" : "不合格";
            }
          },
        },
        {
          title: "图片",
          key: "photoUrl",
          scopedSlots: { customRender: "photoUrl" },
        },
      ],
    };
  },
  computed: {},


  created() {
    this.initSampingMethodList();
    this.initQuestionLevelList();
    this.mqListener();

    // this.initButtonList();
    // this.initColumns();
    // this.setdefColumns();
    //this.initData();
  },

  methods: {
    mqListener(){
     /* console.log(this.$route.query.id)*/

     /* console.log(this.mqVisible)
      console.log(this.mqLoad)
      console.log(this.mqData)*/
      this.initData(this.$route.query);
    },
    backToIndex(){
      let mqData = this.mqData;
      this.$emit('update:mqVisible', 1);
      this.$emit('update:mqLoad', false);
      mqData.id = 0;
      mqData.inMethod = "";
      this.$emit('update:mqData', mqData);
    },
    initQuestionLevelList(){
      getAction('/basic-business/sys/dict/queryDictItemsByCode',{dictCode:'mes_err_level'}).then(res=>{
        if(res.success){
          this.questionLevelList = res.data;
          this.questionLevelMap = this.listToMap(this.questionLevelList,'value');
        }
      })
    },
    initSampingMethodList(){
      getAction('/basic-business/sys/dict/queryDictItemsByCode',{dictCode:'rc_inspect_method'}).then(res=>{
        if(res.success){
          this.sampingMethodList = res.data;
          this.sampingMethodMap = this.listToMap(this.sampingMethodList,'value');
        }
      })
    },
    //封装listToMap
    listToMap(list,name){
      let map = {};
      for(let i in list){
        map[list[i][name]] = list[i];
      }
      return map;
    },
    dayjs,
    /**
     * 展开图标
     * @param props
     * @returns {string|*}
     */
    expandIcon(props) {
      if (props.record.detailList && props.record.detailList.length > 0) {
        if (props.expanded) {
          return (
            <p-icon
              type="minus-square"
              onClick={(e) => {
                props.onExpand(props.record, e);
              }}
            />
          );
        } else {
          return (
            <p-icon
              type="plus-square"
              onClick={(e) => {
                props.onExpand(props.record, e);
              }}
            />
          );
        }
      } else {
        return "";
      }
    },

    /**
     * 初始化加载数据
     */
    initData(query) {

      if (!query.id) {
        this.$message.warning("请通过来料检任务清单选择单号进入！");
      } else {
        let id = query.id;


        getAction("/srm-procurement/auto/srmPcShorderPlus/querylist1?list1Id="+id).then((res1) => {
          if (res1.success) {



           /* this.initAddLineData(res.data);*/
            postAction('/mes-quality/mesQcIncomeBasicInfo/querySampleTemplateList',res1.data).then(res=>{
              if(res.success){

                let d = res.data;
                let data = {
                  custno:d.supno,
                  custname:d.supname,
                  mcode:d.mcode,
                  mname:d.mname,
                  sampingMethod:d.simpleMethod,
                  aqlSimpleStandardName:d.aqlSimpleStandardName,
                  sendBillno:d.billno,
                  sendBillnoLine:d.shAutoid,
                  sendInspectQty:d.finishqtyDelivery,
                  matSn:1,
                  matSnText:d.typeidControl,
                  typeidDelivery:d.typeidDelivery,//srm参数，null不返回，需加入至数据库
                  simpleQty:d.simpleQty,

                }

                let snList = [];

                if(d.snmaterials&&d.snmaterials.length>0){
                  let sKey = 1;
                  d.snmaterials.forEach(item=>{
                    snList.push({key:sKey,sn:item});
                    sKey = sKey +1;
                  })
                }

                this.proSnDetailData  = snList;


                data.sendInspectTime  =  dayjs().format('YYYY-MM-DD HH:mm:ss')
                data.inspectResult = 2;
                data.inMethod =  data.typeidDelivery == 'zf'?2:1;
                data.sampingMethodText =  data.sampingMethod?this.sampingMethodMap[data.sampingMethod]?this.sampingMethodMap[data.sampingMethod].title:'':'';
                this.baseInfoData  =  data;
                console.log(this.baseInfoData)
                let newLineDetail = d.allCheckProjectList;
                this.templateList = newLineDetail;


                if(d.isInspect==1){
                  this.finalFlag = true;
                  this.tipsModalVisible = true;
                }
              }else{
                this.$message.warning(res.msg)
              }
            })



         /*   let param = {
              sendBillno:data.sendBillno,
              sendBillnoLine:data.sendBillnoLine,
              mcode:data.mcode,
            }


*/
            /*this.baseInfoData = data;
            this.individualJudgmentChange();
            this.initSampleTemplateList(data.sendBillno);*/
          } else {
            this.$message.warning(res1.msg);
          }
        });
      }
    },

    initAddLineData(data){
      postAction('/mes-quality/mesQcIncomeBasicInfo/querySampleTemplateList',data).then(res=>{
        if(res.success){
          let data = res.data;
          this.baseInfoData.simpleQty = data.simpleQty
          console.log(this.baseInfoData)
          let newLineDetail = data.allCheckProjectList;
        }else{
          this.$message.warning(res.msg)
        }
      })
    },


    /**
     *加载检验项目列表
     */
    initSampleTemplateList(sendBillno) {
      getAction(
        "/mes-quality/mesQcIncomeBasicInfo/querySampleTemplateList/" +
          sendBillno
      ).then((res) => {
        if (res.success) {
          let data = res.data;
          this.templateList = data;
        } else {
          this.$message.warning(res.msg);
        }
      });
    },
    /**
     * 删除检验项目
     */
    deleteCheckProject(id) {
      const dataSource = [...this.checkDataSource];
      this.checkDataSource = dataSource.filter(item => item.id !== id);
      this.individualJudgmentChange();
     /* let id = e.id;
      let key = e.key;

      let list = this.baseInfoData.checkProjectList;
      list.splice(key-1,1);

      Vue.set(this.baseInfoData,'checkProjectList',list);

      console.log(list)
      console.log(this.baseInfoData.checkProjectList)
*/
/*
      let newData = this.baseInfoData.checkProjectList.filter(function (item) {
        return item.id !== id//返回你选中删除之外的所有数据
      })
      this.baseInfoData.checkProjectList = newData;
      console.log(newData)
      console.log(this.baseInfoData.checkProjectList)*/
      /*const dataSource = [...this.baseInfoData.checkProjectList];
      this.baseInfoData.checkProjectList = dataSource.filter(item => item.id !== id);*/

    },

    /**
     * 点击‘新增行’按钮
     */
    addCheckProjectBtn() {
      this.addCPModalVisible = true;
    },

    openProSNDetailBtn(){
      this.proSnDetailVisible  = true;
    },

    /**
     * 确定添加
     */
    sureAddCheckProject() {
      let data = this.selectedRows[0];
      this.resetRadioSelect();
      this.addCPModalVisible = false;

      console.log(data)


      this.checkDataSource.push(data);

      this.individualJudgmentChange();

    },
    /**
     * 取消-关闭modal
     */
    closeAddCPModal() {
      this.resetRadioSelect();
      this.addCPModalVisible = false;
    },
    closeProSnDetail(){
      this.proSnDetailVisible = false;
    },


    /**
     * 重设radio
     */
    resetRadioSelect() {
      this.selectedRowKeysArray = [];
      this.selectedRows = [];
      this.templateListDetails = [];
      this.selectedType = 1;
    },

    /**
     * 保存按钮
     */
    saveDataBtn() {
      let data = this.baseInfoData;
      putAction(
        "/mes-quality/mesQcIncomeBasicInfo/entity/operation",
        data
      ).then((res) => {
        if (res.success) {
          this.$message.success("保存成功");
          this.initData(this.$route.query);
        } else {
          this.$message.warning(res.msg);
        }
      });
    },

    /**
     * 提交按钮
     */
    submitData() {
      let data = this.baseInfoData;
      data.checkProjectList =  this.checkDataSource;


      data.tableType= 2;
      data.inMethod = 1;
      let flag = true;

      if (data.simpleQty === "") {
        //为空
        flag = false;
        this.$message.error("请输入抽检数量");
      } else {
        //不为空
        if (data.simpleQty < data.checkProjectList.length) {
          flag = false;
          this.$message.error("检验项目数量不能小于抽检数量！");
        }
      }

      if (data.inspectResult != 1) {
        flag = false;
        this.$message.error("检验结论不合格！");
      }

      if (flag) {
        putAction(
          "/mes-quality/mesQcIncomeBasicInfo/custom/submit/entity",
          data
        ).then((res) => {
          if (res.success) {
            this.$message.success("操作成功");
            this.initData(this.$route.query);
          } else {
            this.$message.warning(res.msg);
          }
        });
      }
    },

    /**
     * 检验项目单选框change事件
     */
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeysArray = selectedRowKeys;
      this.selectedRows = selectedRows;
      this.templateListDetails = selectedRows[0].detailList;
      if (this.templateListDetails.length > 0) {
        this.selectedType = 2;
      } else {
        this.selectedType = 3;
      }
    },

    /**
     * 单项判定更改
     */
    individualJudgmentChange(){
      let basicFlag = true;
      this.checkDataSource.forEach(item=>{
        let flag = true;
        let hasChild = false;
        item.detailList.forEach(item2=>{
          hasChild = true;

          if(!item2.valueType||item2.valueType==""){
            item2.individualJudgment = 2;
          }else{
            if(item2.valueType==2){
              item2.individualJudgment = item2.measuredValue=="PASS"?1:2;
            }else{

              if((item2.upperValue&&item2.upperValue!='')&&(!item2.downValue||item2.downValue=="")){
                if(item2.measuredValue<=item2.upperValue){
                  item2.individualJudgment = 1;
                }else{
                  item2.individualJudgment = 2;

                }
              }else if((item2.downValue&&item2.downValue!='')&&(!item2.upperValue||item2.upperValue=="")){
                if(item2.measuredValue>=item2.downValue){
                  item2.individualJudgment = 1;
                }else{
                  item2.individualJudgment = 2;

                }
              }else{
                if(item2.measuredValue<=item2.upperValue&&item2.measuredValue>=item2.downValue){
                  item2.individualJudgment = 1;
                }else{
                  item2.individualJudgment = 2;

                }
              }

            }
          }


          if(!item2.individualJudgment||item2.individualJudgment==2||item2.individualJudgment==""){
            basicFlag = false;
            flag = false;
          }
        })

        if(hasChild){
          if(flag){
            item.inspectResult = 1;
          }else{
            item.inspectResult = 2;
          }
        }


      })
      if(basicFlag){

        this.baseInfoData.inspectResult = 1;
      }else{

        this.baseInfoData.inspectResult = 2;
      }


      if(this.checkDataSource.length==0){
        this.baseInfoData.inspectResult = 2;
      }

    }
  },
};
</script>
<style scoped>
@import "~@assets/less/common.less";
.title {
  padding: 8px 12px;
  background: #fafafa;
}
.title:not(:first-child) {
  margin-top: 25px;
}
/deep/ .poros-form {
  margin-top: 15px;
}
/deep/ .poros-table-body {
  overflow-x: auto !important;
}
/deep/ .poros-col {
  min-height: 54px;
}
/deep/ .poros-table thead th {
  height: 44px;
}
</style>
